<template>
    <div>
        <Header></Header>
        <HeaderSearch></HeaderSearch>
        <div class="w">
            <div class="crumb">
                <div class="crumb-con">
                    <a class="link" href="./index">HappyLaiShop</a>
                    <span>>></span>
                    <span class="link-text">商品详情</span>
                </div>
            </div>
            <div class="intro-wrap">
                <div class="p-img-con">
                    <div class="main-img-con">
                        <img class="main-img" src="http://img.happymmall.com/36bdb49c-72ae-4185-9297-78829b54b566.jpeg" />
                    </div>
                    <ul class="p-img-list">
                        <li class="p-img-item">
                            <img class="p-img" src="http://img.happymmall.com/ac3e571d-13ce-4fad-89e8-c92c2eccf536.jpeg" />
                        </li>
                        <li class="p-img-item">
                            <img class="p-img" src="http://img.happymmall.com/36bdb49c-72ae-4185-9297-78829b54b566.jpeg" />
                        </li>
                        <li class="p-img-item">
                            <img class="p-img" src="http://img.happymmall.com/4bb02f1c-62d5-48cc-b358-97b05af5740d.jpeg" />
                        </li>
                    </ul>
                </div>
                <div class="p-info-con">
                    <h1 class="p-name">{{name}}</h1>
                    <p class="p-subtitle">{{subtitle}}</p>
                    <div class="p-info-item p-price-con">
                        <span class="label">价格:</span>
                        <span class="info">￥{{price}}</span>
                    </div>
                    <div class="p-info-item">
                        <span class="label">库存:</span>
                        <span class="info">{{stock}}</span>
                    </div>
                    <div class="p-info-item p-count-con">
                        <span class="label">数量:</span>
                        <input class="p-count" :value="count" readonly=""/>
                        <span class="p-count-btn plus" @click="plus()">+</span>
                        <span class="p-count-btn minus" @click="minus()">-</span>
                    </div>
                    <div class="p-info-item">
                        <a class="btn cart-add" @click="addToCart()">加入购物车</a>
                    </div>
                </div>
            </div>
            <div class="detail-wrap">
                <div class="detail-tab-con">
                    <ul class="tab-list">
                        <li class="tab-item active">详细描述</li>
                    </ul>
                </div>
                <div class="detail-con">
                    {{detail}}
                </div>
            </div>
        </div>
        <Footer></Footer>
    </div>
</template>

<script>
import {doAddCart, doGetCartProductCount} from '@/api/commonApi'
import Header from "@/views/layout/header.vue";
import Footer from "@/views/layout/footer.vue";
import HeaderSearch from '@/views/layout/header-search.vue';
import store from "@/store";
export default {
    name: "index",
    components: {Header, Footer, HeaderSearch},
    data: function() {
      return {
        isLoading: false,
        detail: "商品详情",
        stock: 888,
        price: 888,
        subtitle: "电冰箱",
        name: "美的电冰箱",
        count: 1,
        productId: ""
      }
    },
    mounted: function(){
        var _this = this;
        // 图片预览
        $(document).on('mouseenter', '.p-img-item', function(){
            var imageUrl   = $(this).find('.p-img').attr('src');
            $('.main-img').attr('src', imageUrl);
        });
        _this.productId = Tool.getUrlParam('productId')
    },
    methods: {
        plus(){
            this.count = this.count + 1 > this.stock ? this.stock : this.count + 1;
        },
        minus(){
            this.count = this.count - 1 < 1 ? 1 : this.count - 1;
        },
        addToCart(){
            let formData = {
                "productId": this.productId,
                "count": this.count
            }
            doAddCart(formData).then((res) => {
                if(res.data.status === 200){
                    Toast.success("加入购物车成功");
                    doGetCartProductCount().then((res) => {
                        if(res.data.status === 200){
                            store.commit("setCartCount", res.data.data)
                            this.$router.push("./result.html?type=cart-add")
                        }
                    })
                } else {
                    Toast.warning(res.data.message);
                }
            })
        }
    }
}
</script>

<style scoped>
/* 面包屑导航 */
.crumb{
    margin-bottom: 15px;
    height: 50px;
    line-height: 50px;
    background: #eee;
    color: #888;
    font-size: 14px;
    border-top: 2px solid #c60023;
}
/* 图片区 */
.intro-wrap{
    overflow: hidden;
}
.intro-wrap .p-img-con{
    float: left;
    width: 420px;
}
/* 主图 */
.intro-wrap .p-img-con .main-img-con{
    height: 400px;
    border: 1px solid #ddd;
}
.intro-wrap .p-img-con .main-img-con .main-img{
    height: 100%;
    width: 100%;
}
/* 缩略图 */
.intro-wrap .p-img-con .p-img-list{
    overflow: hidden;
    height: 76px;
    margin-top: 5px;
    margin-right: -10px;
}
.intro-wrap .p-img-con .p-img-item{
    float: left;
    margin-right: 10px;
    width: 72px;
    height: 72px;
    border: 2px solid #eee;
    cursor: pointer;
}
.intro-wrap .p-img-con .p-img-item:hover{
    border: 2px solid #c60023;
}
.intro-wrap .p-img-con .p-img-item .p-img{
    width: 100%;
    height: 100%;
}
/* 商品信息 */
.intro-wrap .p-info-con{
    float: left;
    width: 640px;
    margin-left: 20px;
}
.intro-wrap .p-info-con .p-name{
    font-size: 20px;
    color: #333;
}
.intro-wrap .p-info-con .p-subtitle{
    font-size: 15px;
    color: #c60023;
    line-height: 30px;
}
.intro-wrap .p-info-con .p-info-item{
    position: relative;
    padding: 0 10px;
    margin-top: 20px;
}
.intro-wrap .p-info-con .p-info-item .label{
    display: inline-block;
    width: 50px;
    color: #999;
}
/* 价格部分 */
.intro-wrap .p-info-con .p-price-con{
    background: #eee;
    padding: 10px;
}
.intro-wrap .p-info-con .p-price-con .info{
    color: #c60023;
    font-size: 18px;
}
/* 数量部分 */
.intro-wrap .p-info-con .p-count-con .p-count{
    height: 36px;
    line-height: 36px;
    font-size: 15px;
    width: 45px;
    text-align: center;
    outline: none;
    border: 1px solid #aaa;
}
.intro-wrap .p-info-con .p-count-con .p-count-btn{
    position: absolute;
    left: 115px;
    display: block;
    width: 20px;
    height: 15px;
    line-height: 15px;
    text-align: center;
    font-size: 10px;
    border: 1px solid #aaa;
    background: #fff;
    cursor: pointer;
    -moz-user-select : none;
    -webkit-user-select : none;
    -ms-user-select : none;
    user-select : none;
}
.intro-wrap .p-info-con .p-count-con .p-count-btn.plus{
    top: 0;
}
.intro-wrap .p-info-con .p-count-con .p-count-btn.minus{
    bottom: 0;
}
/* 商品详细信息 */
.detail-wrap{
    margin-top: 15px;
}
.detail-wrap .tab-list{
    background: #eee;
    border: 1px solid #ddd;
    overflow: hidden;
    border-bottom: 1px solid #c60023;
}
.detail-wrap .tab-list .tab-item{
    float: left;
    width: 100px;
    height: 50px;
    line-height: 50px;
    font-size: 16px;
    text-align: center;
    cursor: pointer;
}
.detail-wrap .tab-list .tab-item.active{
    background: #c60023;
    color: #fff;
}
.detail-wrap .detail-con{
    padding-top: 20px;
    background: #fff;
}
.detail-wrap .detail-con img{
    display: block;
    margin: 0 auto;
}
/* 错误信息 */
.err-tip{
    height: 60px;
    line-height: 60px;
    color: #f60;
    font-size: 16px;
    font-weight: bold;
    text-align: center;
    background: #eee;
}
</style>